package com.aiss.ws.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.TextView;

import com.aiss.ws.R;
import com.aiss.ws.utils.Command;
import com.aiss.ws.utils.Futil;
import com.aiss.ws.utils.Url;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by chenqiang on 16/5/17.
 */
@EActivity(R.layout.activity_logininfor)
public class LoginInforActivity extends BaseActivity {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==Command.HANDLECODE1){


                try {
                    showMessage("登陆成功");
                    JSONObject obj = (JSONObject) msg.obj;
                    JSONObject jsonObject = obj.getJSONObject("return_data");
                    String member_id = jsonObject.getString("member_id");
                    String session_key = jsonObject.getString("session_key");
                    String name = jsonObject.getString("name");
                    Futil.saveValue(context,Command.MEMBER_ID,member_id,2);
                    Futil.saveValue(context,Command.SESSION_KEY,session_key,2);
                    Futil.saveValue(context,Command.NAME,name,2);
                    if(LoginActivity_.getActivity()!=null&&!LoginActivity_.getActivity().isFinishing()){
                        LoginActivity_.getActivity().finish();
                    }
                    if(RegistActivity_.getActivity()!=null&&!RegistActivity_.getActivity().isFinishing()){
                        RegistActivity_.getActivity().finish();
                    }

                    finish();

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }
    };

    @ViewById(R.id.title)
    TextView title_tv;


    @Click(R.id.back)
    void setBack(){
        finish();
    }

    @AfterViews
    void init(){
        title_tv.setText("补全资料");
    }

    private Context context = this;

    @Extra("")
    String user;

    @Extra("")
    String password1;

    @Extra("")
    String password2;

    @ViewById(R.id.title)
    TextView title;

    @ViewById(R.id.qq)
    EditText qq;


    @ViewById(R.id.phone)
    EditText phone;


    @Click(R.id.next_button)
    void next_button(){
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("type","register");
        hashMap.put("name",user);
        hashMap.put("password",password1);
        hashMap.put("password_again",password2);
//        hashMap.put("trname",name.getText().toString());
        hashMap.put("phone",phone.getText().toString());
        hashMap.put("qq",qq.getText().toString());
        Futil.xutils(Url.LOGIN,hashMap,handler, Command.HANDLECODE1);
    }




}
